<?php
namespace Hong\Webdav;

trait TraitPropfind
{
    /**
	 * 获取资源属性 (PROPFIND)
	 * @param string $path 资源路径
	 * @param int $depth 深度 (0-仅资源自身, 1-资源及其直接子项)
	 * @param array|null $properties 要查询的自定义属性列表，null表示默认基本信息
	 * @return array 资源属性数组
	 * @throws \RuntimeException 当请求失败时抛出异常
	 */
	public function propfind($path, $headers, array $properties = null)
	{
		$xmlRequest = '<?xml version="1.0" encoding="utf-8" ?>';
		$xmlRequest .= '<d:propfind xmlns:d="DAV:">';

		if ($properties === null) {
			$xmlRequest = null;
		} else {
			$xmlRequest .= '<d:prop>';
			foreach ($properties as $prop) {
				$xmlRequest .= "<d:$prop/>";
			}
			$xmlRequest .= '</d:prop>';
			$xmlRequest .= '</d:propfind>';
		}

		$headers[] = 'Content-Type: application/xml; charset="utf-8"';

		$response = $this->sendRequest('PROPFIND', $path, $xmlRequest, $headers);

		if ($response['status'] !== 207) { // 207 Multi-Status
			throw new \RuntimeException("PROPFIND request failed. Status: " . $response['status']);
		}

		return $this->parsePropfindResponse($response['body']);
	}
}